Side effects

Even if the functional programming style prefers writing pure functions without side-effects, sometimes side-effects are the reason for running the function in the first place.

This section lists the most important functions with side-effects that are defined in the SCL standard library.

Printing

print :: Show a => a -> <Proc> () (Prelude)

Prints the given value in the console.

printString :: String -> <Proc> () (Prelude)

Prints the given string to the console.

printError :: String -> <Proc> () (Prelude)

Prints an error message to the console.

printingToFile :: String -> <b> a -> <b> a (Prelude)

printingToFile "fileName" expression executes the expression so that all its console prints are written to the file given as a first parameter. The file will be written with UTF-8 encoding.

References

ref :: a -> <Proc> Ref a (Prelude)

Creates a new reference with the given initial value.

getRef :: Ref a -> <Proc> a (Prelude)

Returns the current value of the reference.

(:=) :: Ref a -> a -> <Proc> () (Prelude)

Sets a new value for the reference.

Mutable arrays

data T a (ArrayList)

Type of lists.

new :: () -> <Proc> ArrayList.T a (ArrayList)

Constructs a new list.

add :: ArrayList.T a -> a -> <Proc> () (ArrayList)

Adds an element to the list.

remove :: ArrayList.T a -> Integer -> <Proc> a (ArrayList)

Removes the i:th element of the list

get :: ArrayList.T a -> Integer -> <Proc> a (ArrayList)

Gets the i:th element of the list.

length :: ArrayList.T a -> <Proc> Integer (ArrayList)

The current length of the list.

contains :: ArrayList.T a -> a -> <Proc> Boolean (ArrayList)
iter :: (a -> <b> ()) -> ArrayList.T a -> <Proc,b> () (ArrayList)

Iterates thru the list. The elements added during the iteration are also iterated.

for :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> () (ArrayList)

'iter' with swapped parameter.

mapInPlace :: (a -> <b> a) -> ArrayList.T a -> <Proc,b> ArrayList.T a (ArrayList)

Replaces every element of the list by the result of applying the element to the given function.

popUntilEmpty :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> () (ArrayList)

Pops the last element of the list until list becomes empty.

Mutable maps and sets

In addition to mutable arrays, SCL provides mutable map and set structures:

data T a b (MMap)
data T a (MSet)

Both support the usual insert/lookup/remove operations. When the mutable phase is complete, convert to an immutable value with freeze.

Escaping side-effects

runProc :: <Proc,b> a -> <b> a (Builtin)

Warning: runProc suppresses the <Proc> effect, making a side-effecting computation appear pure to the type system. This is dangerous in production code because the compiler may optimise away or reorder calls that it believes have no observable effects. Only use runProc in tightly controlled situations where you are certain reordering cannot happen.

Transactions and effect conversion

syncRead and syncWrite (from Simantics/DB) consume the <ReadGraph> and <WriteGraph> effects respectively, converting them to <Proc>. This is typically used when you need to perform database operations from within a <Proc> context. See 2.05 Semantic graph for details.

Effects propagate upward: if a function calls another function with effect <E>, the caller must also declare <E> in its type signature (unless it explicitly consumes the effect with a combinator like syncRead).

Monad sequencing with mdo

When working with monadic types (lists, Maybe, sequences), mdo provides a clean sequencing syntax without explicit >>= chains. See 1.15 Functors and monads for a full explanation.